Add Dockerfile auto-discovery when scanning is enabled#19
Add Dockerfile auto-discovery when scanning is enabled#19
Conversation
When Dockerfile scanning is enabled but no explicit `dockerfiles` config is provided, automatically search the workspace for Dockerfiles instead of skipping the scan entirely. Discovery patterns: - Dockerfile (exact match) - Dockerfile.* (e.g., Dockerfile.prod, Dockerfile.dev) - *.dockerfile (e.g., app.dockerfile) Excluded directories to avoid false positives: - node_modules, vendor, .git - test, tests, testing, __tests__, spec, specs - fixture, fixtures, testdata, test_data - example, examples, sample, samples - dist, build, out, target - venv, .venv, env, .env - app_tests (Socket Basics test fixtures) This improves the customer experience by eliminating the need to manually specify Dockerfile paths in each repo, while still respecting explicit configuration when provided. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
| return [] | ||
|
|
||
| # Directories to exclude from search | ||
| exclude_dirs = { |
There was a problem hiding this comment.
should exclude dirs based on content of .gitignore
There was a problem hiding this comment.
Good point. Using .gitignore would make exclusions project-specific rather than hardcoded.
A few considerations:
-
Parsing complexity - .gitignore supports wildcards, negations (
!keep.txt), directory-only patterns, and nested files. Would need a library likepathspecto handle it correctly. -
Different intent - .gitignore defines what Git shouldn't track, not necessarily what shouldn't be scanned. A Dockerfile in an ignored test fixture could still have real security issues worth catching.
-
Missing/minimal .gitignore - Not all repos have one, so we'd still need a fallback list.
For this PR, the hardcoded list provides predictable baseline behavior. Could add .gitignore support as a follow-up with a flag like respect_gitignore: true for users who want it.
Open to discussing if you feel strongly this should block the PR.
- Dockerfile with simple Python app (has USER root finding) - GitHub Actions workflow with two test cases: - Toggle OFF: No dockerfiles input - Toggle ON: dockerfiles=Dockerfile - README explaining test cases Tests behavior from SocketDev/socket-basics#19 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Smoke Test ResultsRan a live CI test to verify the Dockerfile scanning toggle behavior. Test Repo: dc-larsen/socket-basics-smoke-test Test 1: Toggle OFF (no
|
|
Closing in favor of native GitHub Action flow for Dockerfile discovery. See Doug's recommendation from Jan 29 - this approach is more portable and doesn't require changes to Socket Basics. |
Problem
Customers currently need to manually specify Dockerfile paths in their config:
This is tedious and error-prone, especially across multiple repos where Dockerfiles may be in different locations.
Solution
Auto-discover Dockerfiles when scanning is enabled but no explicit paths are configured.
Before: Customer must know and list every Dockerfile path
After: Just enable scanning, Socket Basics finds them automatically
Implementation
Added
_discover_dockerfiles()method to the Trivy connector that:os.walk()Discovery Patterns
DockerfileDockerfile.prod,Dockerfile.dev,Dockerfile.testapp.dockerfile,backend.dockerfileExcluded Directories
Directories are excluded case-insensitively to avoid false positives:
node_modules,vendor.git,.svn,.hgtest,tests,testing,__tests__,spec,specsfixture,fixtures,testdata,test_dataexample,examples,sample,samplesmock,mocksdist,build,out,targetvenv,.venv,env,.env.cache,.tox,.nox,.pytest_cacheapp_testsBehavior
dockerfilesexplicitly setdockerfilesempty + scanning enableddockerfilesempty + scanning disabledTest Coverage
15 unit tests covering all discovery and exclusion scenarios:
Test Details
discovers_dockerfile_at_rootDockerfilein workspace rootdiscovers_dockerfile_in_subdirectorydocker/Dockerfilenested pathsdiscovers_dockerfile_with_suffixDockerfile.prod,Dockerfile.devdiscovers_dockerfile_extensionapp.dockerfile,backend.dockerfileexcludes_node_modulesnode_modules/**/Dockerfileexcludes_vendor_directoryvendor/**/Dockerfileexcludes_test_directoriestest/,tests/,testing/,__tests__/excludes_fixture_directoriesfixture/,fixtures/,testdata/excludes_example_directoriesexample/,examples/,sample/,samples/excludes_build_directoriesdist/,build/,out/,target/excludes_app_tests_directorydiscovers_multiple_dockerfilesempty_workspace_returns_empty_listno_dockerfiles_returns_empty_list[]when no Dockerfiles existcase_insensitive_exclusionsNode_Modules,VENDOR,Testsall excludedFiles Changed
socket_basics/core/connector/trivy/trivy.py- Added_discover_dockerfiles()method and integrationtests/test_dockerfile_discovery.py- Pytest test suite (for future use with Python 3.10+)tests/run_discovery_tests.py- Standalone test runner (works with any Python 3.x)Example Log Output
🤖 Generated with Claude Code